package devices

import (
	"math/rand"
	"strings"
)

// DeviceBrand 设备品牌类型
type DeviceBrand string

// 设备品牌常量
const (
	Apple   DeviceBrand = "Apple"
	Samsung DeviceBrand = "Samsung"
	Xiaomi  DeviceBrand = "Xiaomi"
	Huawei  DeviceBrand = "Huawei"
	Oppo    DeviceBrand = "OPPO"
	Vivo    DeviceBrand = "Vivo"
	OnePlus DeviceBrand = "OnePlus"
	Realme  DeviceBrand = "Realme"
	Nokia   DeviceBrand = "Nokia"
	Sony    DeviceBrand = "Sony"
)

// DeviceInfo 设备信息结构体
type DeviceInfo struct {
	Brand   DeviceBrand
	Model   string
	Type    string // phone, tablet
	Code    string // 设备代码，如SM-G975F
}

// GetModelsByBrand 根据品牌获取所有型号
func GetModelsByBrand(brand DeviceBrand) []string {
	switch brand {
	case Apple:
		return getAllAppleModels()
	case Samsung:
		return getAllSamsungModels()
	case Xiaomi:
		return getAllXiaomiModels()
	case Huawei:
		return getAllHuaweiModels()
	case Oppo:
		return getAllOppoModels()
	default:
		return []string{}
	}
}

// GetRandomModelByBrand 随机获取指定品牌的型号
func GetRandomModelByBrand(brand DeviceBrand) string {
	models := GetModelsByBrand(brand)
	if len(models) == 0 {
		return "Unknown Device"
	}
	return models[rand.Intn(len(models))]
}

// GetRandomDevice 随机获取一个设备信息
func GetRandomDevice() DeviceInfo {
	brands := []DeviceBrand{Apple, Samsung, Xiaomi, Huawei, Oppo}
	brand := brands[rand.Intn(len(brands))]
	
	model := GetRandomModelByBrand(brand)
	
	// 根据型号判断设备类型
	deviceType := "phone"
	if strings.Contains(strings.ToLower(model), "ipad") || 
	   strings.Contains(strings.ToLower(model), "tab") || 
	   strings.Contains(strings.ToLower(model), "pad") {
		deviceType = "tablet"
	}
	
	return DeviceInfo{
		Brand: brand,
		Model: model,
		Type:  deviceType,
		Code:  getDeviceCode(brand, model),
	}
}

// GetAllBrands 获取所有支持的品牌
func GetAllBrands() []DeviceBrand {
	return []DeviceBrand{Apple, Samsung, Xiaomi, Huawei, Oppo, Vivo, OnePlus, Realme, Nokia, Sony}
}

// GetBrandsByOS 根据操作系统获取对应的品牌列表
func GetBrandsByOS(osType string) []DeviceBrand {
	switch osType {
	case "ios":
		return []DeviceBrand{Apple}
	case "android":
		return []DeviceBrand{Samsung, Xiaomi, Huawei, Oppo, Vivo, OnePlus, Realme, Nokia, Sony}
	default:
		return []DeviceBrand{Apple, Samsung, Xiaomi, Huawei, Oppo, Vivo, OnePlus, Realme, Nokia, Sony}
	}
}

// GetOSByBrand 根据品牌获取对应的操作系统
func GetOSByBrand(brand DeviceBrand) string {
	switch brand {
	case Apple:
		return "ios"
	case Samsung, Xiaomi, Huawei, Oppo, Vivo, OnePlus, Realme, Nokia, Sony:
		return "android"
	default:
		return "android"
	}
}

// FilterBrandsByOS 根据操作系统过滤品牌列表
func FilterBrandsByOS(brands []DeviceBrand, osType string) []DeviceBrand {
	if osType == "" {
		return brands
	}
	
	var filtered []DeviceBrand
	for _, brand := range brands {
		if GetOSByBrand(brand) == osType {
			filtered = append(filtered, brand)
		}
	}
	return filtered
}

// FilterModelsByBrand 根据品牌过滤型号映射
func FilterModelsByBrand(models map[DeviceBrand][]string, brand DeviceBrand) map[DeviceBrand][]string {
	filtered := make(map[DeviceBrand][]string)
	if models, ok := models[brand]; ok {
		filtered[brand] = models
	}
	return filtered
}

// getDeviceCode 获取设备代码
func getDeviceCode(brand DeviceBrand, model string) string {
	// 这里可以根据品牌和型号返回对应的设备代码
	// 例如三星的SM-G975F等
	return ""
}

// getAllAppleModels 获取所有苹果型号
func getAllAppleModels() []string {
	var models []string
	for _, m := range AppleModels["iPhone"] {
		models = append(models, m)
	}
	for _, m := range AppleModels["iPad"] {
		models = append(models, m)
	}
	return models
}

// getAllSamsungModels 获取所有三星型号
func getAllSamsungModels() []string {
	var models []string
	for _, category := range SamsungModels {
		for _, m := range category {
			models = append(models, m)
		}
	}
	return models
}

// getAllXiaomiModels 获取所有小米型号
func getAllXiaomiModels() []string {
	var models []string
	for _, category := range XiaomiModels {
		for _, m := range category {
			models = append(models, m)
		}
	}
	return models
}

// getAllHuaweiModels 获取所有华为型号
func getAllHuaweiModels() []string {
	var models []string
	for _, category := range HuaweiModels {
		for _, m := range category {
			models = append(models, m)
		}
	}
	return models
}

// getAllOppoModels 获取所有OPPO型号
func getAllOppoModels() []string {
	var models []string
	for _, category := range OppoModels {
		for _, m := range category {
			models = append(models, m)
		}
	}
	return models
}